﻿using System.Drawing;
using System.Windows.Forms;

namespace RegexSharp.Control
{
    public class RSRichTextBox : RichTextBox
    {
        private Color _defaultBackColor;
        private Color _highlightColor = Color.FromName(Config.Global.HighlightColor);

        public RSRichTextBox()
        {
            _defaultBackColor = this.BackColor;
        }

        private void HighlightWithoutClearUndo(int index, int length)
        {
            this.Select(index, length);
            this.SelectionBackColor = _highlightColor;
        }

        // Public Methods

        private delegate void HighlightCallBack(Core.HighlightInfo hlinfo);
        public void Highlight(Core.HighlightInfo hlinfo)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new HighlightCallBack(Highlight), new object[] { hlinfo });
            }
            else
            {
                HighlightWithoutClearUndo(hlinfo.Index, hlinfo.Length);

                this.ClearUndo();
            }
        }

        private delegate void HighlightCallBack2(Core.HighlightInfo[] hlinfos);
        public void Highlight(Core.HighlightInfo[] hlinfos)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new HighlightCallBack2(Highlight), new object[] { hlinfos });
            }
            else
            {
                foreach (Core.HighlightInfo hlinfo in hlinfos)
                {
                    HighlightWithoutClearUndo(hlinfo.Index, hlinfo.Length);
                }

                this.ClearUndo();
            }
        }

        private delegate void ClearAllHighlightsCallBack();
        public void ClearAllHighlights()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ClearAllHighlightsCallBack(ClearAllHighlights));
            }
            else
            {
                this.SelectAll();
                this.SelectionBackColor = _defaultBackColor;
                this.Select(0, 0);

                this.ClearUndo();
            }
        }

        private delegate void SetTextCallBack(string text, params object[] args);
        public void SetText(string text, params object[] args)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetTextCallBack(SetText), new object[] { text, args });
            }
            else
            {
                if (args.Length > 0)
                {
                    this.Text = string.Format(text, args);
                }
                else
                {
                    this.Text = text;
                }
            }
        }

        private delegate void SetAppendTextCallBack(string text, params object[] args);
        public void SetAppendText(string text, params object[] args)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetAppendTextCallBack(SetAppendText), new object[] { text, args });
            }
            else
            {
                if (args.Length > 0)
                {
                    this.AppendText(string.Format(text, args));
                }
                else
                {
                    this.AppendText(text);
                }
            }
        }
    }
}
